package threadUtils.demo03;

import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;

public class ExchangerDemo01 {

    public static void main(String[] args) {

        final Exchanger<String> stringExchanger = new Exchanger<>();

        for (int i = 0; i < 4; i++) {
            new Thread(() -> {

                System.out.println(Thread.currentThread().getName() + " start");

                try {
                    TimeUnit.MILLISECONDS.sleep(20);
                    String exchange = stringExchanger.exchange(Thread.currentThread().getName() + "发送的");
                    System.out.println(Thread.currentThread().getName() +"接收到：" + exchange);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "X" + i).start();
        }
    }

}
